package com.zhang.activity;

import com.zhang.little_sample.R;
import com.zhang.little_sample.R.color;
import com.zhang.little_sample.R.dimen;

import android.R.integer;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FlyDialog extends Dialog {
	public interface OnFlyDialogListener {
		// 定义回调事件
		public void back(int resultTag);
	}

	private OnFlyDialogListener onFlyDialogListener;
	private LinearLayout mLayout;
	private Context context;
	private int resultTag;

	public FlyDialog(Context context, OnFlyDialogListener onFlyDialogListener) {
		super(context);
		this.context = context;
		this.onFlyDialogListener = onFlyDialogListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.flydialog);
		initChildViews();
	}
	//初始化控件
	private void initChildViews() {
		mLayout = (LinearLayout) findViewById(R.id.linear_track);
		TextView tvSlider = new TextView(context);
		tvSlider = (TextView) mLayout.findViewById(R.id.tv_slider);
		tvSlider.setOnTouchListener(new SlideOnTouchListener());
	}

	// 目标功能 位置大于 1/4 3/4时 吸附边框 否则回到初始位置
	private class SlideOnTouchListener implements OnTouchListener {

		/*
		 * @Override public boolean onTouch(View v, MotionEvent event) { switch
		 * (event.getAction()) { case MotionEvent.ACTION_MOVE: // 随手势移动
		 * v.setLayoutParams(new LayoutParams((int) event.getX(), (int)
		 * event.getY())); mLayout.invalidate(); break; case
		 * MotionEvent.ACTION_UP: if ((int) event.getX() < 50) { // 左吸附
		 * v.setLayoutParams(new LayoutParams(0, (int) event .getY()));
		 * onFlyDialogListener.back(0); } else if ((int) event.getX() > 150) {
		 * // 右吸附 v.setLayoutParams(new LayoutParams(150, (int) event .getY()));
		 * onFlyDialogListener.back(1); } else { // 回到初始位 v.setLayoutParams(new
		 * LayoutParams( 75, (int) event.getY())); onFlyDialogListener.back(2);
		 * } mLayout.invalidate(); FlyDialog.this.dismiss(); break; default:
		 * break; } return true; }
		 */
		
		int temp[] = new int[] { 0, 0 };//位置坐标

		public boolean onTouch(View v, MotionEvent event) {
			int eventAction = event.getAction();
			int x = (int) event.getRawX();//得到发生event时 它在屏幕中的x位置
			int y = (int) event.getRawY();
			switch (eventAction) {
			case MotionEvent.ACTION_DOWN:

				temp[0] = (int) (x - v.getLeft());
				temp[1] = y;
				v.postInvalidate();
				break;
			case MotionEvent.ACTION_MOVE:
				// 限制移动范围
				int left = x - temp[0];
				int top = 0;
				int right = left + v.getWidth();
				int bottom = top + v.getHeight();
				if (left <= (int) mLayout.getLeft()) {
					left = (int) mLayout.getLeft();
					right = left + v.getWidth();
				} else if (right >= (int) mLayout.getRight()) {
					right = (int) mLayout.getRight();
					left = right - v.getWidth();
				}
				v.layout(left, top, right, bottom);
				
				v.postInvalidate();

				break;
			case MotionEvent.ACTION_UP:
				if ((v.getLeft() - mLayout.getLeft()) < mLayout.getWidth() / 6) {
					// 左吸附
					v.layout(mLayout.getLeft(), 0,
							mLayout.getLeft() + v.getWidth(), v.getHeight());
					v.postInvalidate();
					onFlyDialogListener.back(0);
					resultTag=0;
				} else if ((mLayout.getRight() - v.getLeft() - v.getWidth()) < mLayout
						.getWidth() / 6) {
					// 右吸附
					v.layout(mLayout.getRight() - v.getWidth(), 0,
							mLayout.getRight(), v.getHeight());
					v.postInvalidate();
					onFlyDialogListener.back(1);
					resultTag=1;
				} else {
					// 初始位
					v.layout(
							mLayout.getLeft()
									+ (mLayout.getWidth() - v.getWidth()) / 2,
							0,
							mLayout.getRight()
									- (mLayout.getWidth() - v.getWidth()) / 2,
							v.getHeight());
					v.postInvalidate();
					onFlyDialogListener.back(2);
					resultTag=2;
				}
				FlyDialog.this.dismiss();
				doWhat(resultTag);
				break;
			default:
				break;
			}

			return true;
		}
	}
	private void doWhat(int msg) {
		switch (msg) {
		case 0:
			Toast.makeText(context, "确认", Toast.LENGTH_SHORT).show();
			break;
		case 1:
			Toast.makeText(context, "取消", Toast.LENGTH_SHORT).show();
			break;
		case 2:
			
			break;

		default:
			break;
		}
	}
}
